<?php
class Admin_Model_Template extends Core_Database
{
	
 	/**
     * Lay danh sach tat ca cac template
     * @param: void
     * @return : array(): neu co it nhat mot template  null: neu khong co template nao
     * 
     * */
    public function getTemplates()
    {
     $sql = 'select * from template';
     $sth = $this->_db->prepare($sql);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;   
    }
    
    /**
     * Them mot template moi
     * @param: name path description
     * @return: bool  true: thanh cong, false : that bai
     * 
     * */
    public function insertTemplate($name,$path,$description)
    {
        $sql = 'insert into template(template_name,template_path,description) values(:name,:path,:description)';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':name', $name, PDO::PARAM_STR);
    	$sth->bindParam(':path', $path, PDO::PARAM_STR);
    	$sth->bindParam(':description', $description, PDO::PARAM_STR);
    	return $sth->execute();
    }

    /**
     * Lay mot template
     * @param : template_id
     * @return : array : neu ton tai  null : neu khong ton tai
     * */
    public function getTemplate($template_id)
    {
        $sql = 'select * from template where template_id = :template_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':template_id',$template_id,PDO::PARAM_INT);
        $status = $sth->execute();
        if (!$status)
            return null;
        $result = $sth->fetchAll();
        if(count($result)>0)
           return $result;
        else
           return null;  
    }
    
    /**
     * Cap nhat template
     * @param : template_id  name path description
     * @return bool true : thanh cong , false: that bai
     * */
     public function updateTemplate($template_id,$name,$path,$description)
     {
        $sql = 'update template set template_name = :name, template_path = :path, description = :description where template_id = :template_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':template_id',$template_id,PDO::PARAM_INT);
  	    $sth->bindParam(':name', $name, PDO::PARAM_STR);
        $sth->bindParam(':path', $path, PDO::PARAM_STR);
        $sth->bindParam(':description', $description, PDO::PARAM_STR);
    	return $sth->execute();
     }
     
     /**
      * Xoa mot template
      * @param :template_id
      * @return bool  true: thanh cong false: that bai
      * */
      public function deleteTemplate($template_id)
      {
        $sql = 'delete from template where template_id = :template_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':template_id',$template_id,PDO::PARAM_INT);
        return $sth->execute();
      } 
    
}
